<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class GroupCourse extends Model
{
    const WEEKS = [
        1 => '周一',
        2 => '周二',
        3 => '周三',
        4 => '周四',
        5 => '周五',
        6 => '周六',
        7 => '周日',
    ];

    const DEADLINE = [
        0 => '不限制报名时间',
        10 => '上课时间前10分钟',
        30 => '上课时间前30分钟',
        60 => '上课时间前1小时',
        90 => '上课时间前1个半小时',
        120 => '上课时间前2小时',
    ];

    const SCHEDULE_TYPE_WEEK = 1; // 按周排课
    const SCHEDULE_TYPE_DATE = 2; // 按日期排课

    protected $fillable = [
        'tenant_id',
        'coach_id',
        'title',
        'cover_url',
        'price',
        'tags',
        'people_number',
        'start_people_number',
        'schedule_type',
        'week',
        'date',
        'course_start_time',
        'course_end_time',
        'deadline',
        'intro',
        'notes',
        'sort',
        'total_buy_count',
        'is_enable',
    ];

    public function getTagsAttribute($value)
    {
        return explode(',', $value);
    }

    public function setTagsAttribute($value)
    {
        $this->attributes['tags'] = implode(',', $value);
    }

    public function coach()
    {
        return $this->belongsTo(Coach::class);
    }

    public function tenant()
    {
        return $this->belongsTo(Tenant::class);
    }

    public function orders()
    {
        return $this->hasMany(GroupCourseOrder::class);
    }

    // 今日报名数量
    public function todayOrderCount()
    {
        return $this->orders()
            ->where(['is_paid' => true])
            ->where('status', '!=', GroupCourseOrder::STATUS_CANCELED)
            ->whereDate('paid_time', Carbon::today()->toDateString())
            ->count();
    }

    public static function getDeadlineSelectOptions()
    {
        return self::DEADLINE;
    }

    public static function getWeeksSelectOptions()
    {
        return self::WEEKS;
    }
}
